'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, TEXT,DECIMAL } = app.Sequelize;
    const Model = app.model.define(
      'goods',
      {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            name: STRING(255),
            category_id: INTEGER,
            price: DECIMAL(10, 2),
            label_price: {
              type: DECIMAL(10, 2),
                get() {
                    return this.getDataValue('label_price') || null;
                }
            },
            old_price: DECIMAL(10, 2),
            status: INTEGER,
            unit: STRING(255),
            duration: INTEGER,
            suitable_people: STRING(255),
            image: STRING(255),
            photos: TEXT,
            description: TEXT,
            content: TEXT,
            is_recommend: INTEGER,
            is_del: INTEGER,
            sale_count: INTEGER,
            create_time: {
              type: app.Sequelize.DATE,
              allowNull: false,
              get() {
                  return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
              },
            },
            updated_time: {
              type: app.Sequelize.DATE,
              allowNull: false,
              get() {
                  return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
              },
            }
      },
      {
          tableName: 'project',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );

    Model.associate = function () {
        Model.belongsTo(app.model.Category, { foreignKey: 'category_id', targetKey: 'id' })
        Model.hasMany(app.model.GoodsCollect, { foreignKey: 'project_id', targetKey: 'id' })
        Model.hasMany(app.model.OrderGoodsDetail, { foreignKey: 'project_id', targetKey: 'id' })
    }
    return Model
};
